package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class,weight = 100)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    public User findByUsername(String username) {

        // 获取User
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }

        // 获取Role
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);

        // 获取Permission
        if(roles != null && roles.size() > 0){
            for(Role role : roles){
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size() > 0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }

    @Override
    public List<User> findHealthManager() {
        return userDao.findHealthManager();
    }


    //分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //添加检查组合，同时需要设置检查组合和检查项的关联关系
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        setUserAndRole(user.getId(),roleIds);
        System.out.println(user.getId());
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);

    }

    @Override
    public List<Integer> findRoleIdsByUserId(int id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        //根据id删除中间表数据（清理原有关联关系）
        userDao.deleteAssociation(user.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setUserAndRole(user.getId(), roleIds);
        //更新检查组基本信息
        userDao.edit(user);
    }

    @Override
    public void delete(Integer id) {
        //根据id查询关联表删除关联关系
        userDao.deleteAssociation(id);
        userDao.delete(id);
    }

    //设置检查组合和检查项的关联关系
    public void setUserAndRole(Integer userId, Integer[] roleIds){
        if(roleIds != null && roleIds.length > 0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.setUserAndRole(map);
            }
        }
    }



}